<?php

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Users\Form\OfflinemailForm;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;
use Zend\Mail;

/**
 * offlinemailController
 *
 * @author
 *
 * @version
 *
 */
class OfflinemailController extends AbstractActionController {
	/**
	 * The default action - show the home page
	 */
	protected  $authservice;
	
	public function indexAction() {
		// TODO Auto-generated offlinemailController::indexAction() default action
		$userTable = $this->getServiceLocator()->get('Users\Model\UserTable');
		// Get User Info from Session
		$userEmail = $this->getAuthService()->getStorage()->read();
		$user = $userTable->getUserByEmail($userEmail);
		//************************************************************
		  $Users = $userTable->fetchAll();
		  //Convert rowset to array with column name
		  $Names = array();
		    foreach ($Users as $User) {
			     $Names[$User->id] = $User->name;
		    }
		//***********************************************************
		$Form = new OfflinemailForm();
		$Form->get('users')->setValueOptions($Names);
		
		$viewModel = new ViewModel( array('username' => $user->name,'form' => $Form, ));
		
		return $viewModel;
	}
	
	public function getAuthService()
	{
		if (! $this->authservice) {
			$dbAdapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
			$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter,'user','email','password', 'MD5(?)');
			$authService = new AuthenticationService();
			$authService->setAdapter($dbTableAuthAdapter);
			$this->authservice = $authService;
		}
		return $this->authservice;
	}
	
	public function sendAction()
	{
		$form = new OfflinemailForm();
		$request=$this->request;
		//$file=$this->getRequest();
		$form->setData($request->getPost());
		//******getting fromUserId as current logged user*******************
		$userTable = $this->getServiceLocator()->get('Users\Model\UserTable');
		// Get User Info from Session
		$userEmail = $this->getAuthService()->getStorage()->read();
		$user = $userTable->getUserByEmail($userEmail);
		//*******************************************************************
		if ($form->isValid()) {
			$request = $this->getRequest();
			if ($request->isPost()) {
				$msgSubj = $request->getPost()->get('subject');
				$msgText = $request->getPost()->get('message');
				$toUserId = $request->getPost()->get('users');
				$fromUserId = $user->id;
				$this->sendOfflineMessage($msgSubj,$msgText, $fromUserId, $toUserId);
			}
		}
		
		return $this->redirect()->toRoute('offlinemail' , array('action' => 'index'));;
	}
	
	protected function sendOfflineMessage($msgSubj,	$msgText, $fromUserId, $toUserId)
	{
		$userTable = $this->getServiceLocator()->get('UserTable');
		$fromUser = $userTable->getUser($fromUserId);
		$toUser = $userTable->getUser($toUserId);
		$mail = new Mail\Message();
		$mail->setFrom($fromUser->email, $fromUser->name);
		$mail->addTo($toUser->email, $toUser->name);
		$mail->setSubject($msgSubj);
		$mail->setBody($msgText);
		$transport = new Mail\Transport\Sendmail();
		$transport->send($mail);
		return true;
	}
		
}